package com.region.integral.business_user.obtain.dao.impl;

import java.util.List;

import org.springframework.stereotype.Component;

import com.region.integral.business_user.obtain.dao.IIntegralAuthentication;
import com.region.integral.business_user.pojo.BUIEntity;
import com.region.integral.business_user.xml.Regulation;
import com.region.integral.business_user.xml.dto.Authentication;
import com.region.integral.common_user.obtain.vo.IntegralData;
import com.region.integral.exception.IntegralException;
import com.region.integral.hibernate.BaseHibernate;
import com.region.integral.util.BusinessUserLogUtil;
import com.region.integral.util.XMLRegulationUtil;
@Component("iIntegralAuthentication")
public class IntegralAuthenticationImpl extends BaseHibernate implements IIntegralAuthentication {

	@Override
	public void authentication(int userid, String authWay,
			IntegralData integralData) throws IntegralException {
		BUIEntity buiEntity = getBUIEntity(userid);
		if (buiEntity != null) {
			Regulation regulation = XMLRegulationUtil.unmarshaller2();
			List<Authentication> authentications = regulation.getAuthentication();
			for (Authentication authentication : authentications) {
				if (authentication.getAuthWay().equals(authWay)) {
					int currIntegral = buiEntity.getCurrIntegral();
					int maxIntegral = buiEntity.getMaxIntegral();
					int totalIntegral = currIntegral+ authentication.getIntegral();
					buiEntity.setCurrIntegral(totalIntegral);
					if (maxIntegral < totalIntegral) {
						buiEntity.setMaxIntegral(totalIntegral);
					}
					integralData.setSuccee(true);
					hibernateTemplate.update(buiEntity);
					hibernateTemplate.save(BusinessUserLogUtil.log(userid, authentication.getIntegral(), "authentication"));
				}
			}
		}
		
	}

}
